var co = require('co');
var seqModule = require('../../models/index');
var db = new seqModule();
var util = require('../../common/util');


var meetController = {
    getmeetPage: function (req, res) {
        co(function () {
            var page = parseInt(req.query.page);
            var params = {};
            var paramT = {};
            // if (req.query.pTitle != undefined) {
            //     params["$or"] = { "name": { "$like": "%" + req.query.pTitle + "%" }, "tel": { "$like": "%" + req.query.pTitle + "%" } };
            // }
            if (req.query.meetTitle != undefined) {
                params["meetTitle"] = { "$like": "%" + req.query.meetTitle + "%" };
            }
            paramT["where"] = params;
            paramT["order"] = [["createTime", 'DESC']];
            if (req.query.page != undefined && req.query.rows != undefined) {
                paramT["offset"] = parseInt(req.query.page);
                paramT["limit"] = parseInt(req.query.rows);
            }
            db.meetModule.findAndCountAll(paramT).then(function (result) {
                result.page = page;
                res.status(200).json(result);
            });
        }).catch(function (e) {
            res.status(500).json({"message":"服务异常！"});
            console.log(e);
        })
    },  
    getmeetById: function (req, res) {
        co(function () {
            var paramT = {};
            paramT.where = req.params;
            db.meetModule.findOne(paramT).then(function (result) {
                res.status(200).json(result);
            });
        }).catch(function (e) {
            res.status(500).json({"message":"服务异常！"});
            console.log(e);
        })
    },  
    addMeet: function (req, res) {
        co(function () {
            req.body.id = util.getUUID();
            req.body.createTime = util.getNowTime();
            db.meetModule.create(req.body).then(function (result) {
                res.status(200).json(result);
            });
        }).catch(function (e) {
            res.status(500).json({"message":"服务异常！"});
            console.log(e);
        })
    },
    updateMeet: function (req, res) {
        co(function () {
          var paramT = {};
          paramT.where = req.params;
            db.meetModule.update(req.body,paramT).then(function (result) {
                res.status(200).json(result);
            });
        }).catch(function (e) {
            res.status(500).json({"message":"服务异常！"});
            console.log(e);
        })
    },
    delMeetByIds: function (req, res) {
        co(function () {
            var paramT = {};
            var paramU = {};
            var StringId = req.query.params;//传过来的params是以‘,’隔开的字符串
            var arr = StringId.split(",");
            paramU['id'] = [arr];
            paramT['where'] = paramU;
            //destroy、方法就是删除记录的方法 paramT是删除条件
            db.meetModule.destroy(paramT).then(function (results) {
                res.status(200).json(results);
            });
        }).catch(function (e) {
            res.status(500).json({"message":"服务异常！"});
            console.log(e);
        })
    }
}
module.exports = meetController;